<?php
namespace App\Http\Controllers\Api;

use App\Exceptions\BizException;
use App\Model\Upload;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    // 上传图片接口
    public function image(Request $request)
    {
        try {
            if (empty($_FILES))
                throw new BizException('未找到上传文件');

            $width = empty($request->width) ? 500 : $request->width;
            $url = (new Upload('image', $width))->handle($request->getSchemeAndHttpHost());
            return json_encode(['status' => 1, 'info' => $url]);
        } catch (\Exception $e) {
            return json_encode(['status' => 0, 'info' => $e->getMessage()]);
        }
    }
}